// 爆炸效果管理模块
import { Game, canvas, ctx } from './game-state.js';


// 加载爆炸图像
export function loadExplosionImage() {
    return new Promise((resolve, reject) => {
        const img = new Image();
        img.onload = () => resolve(img);
        img.onerror = () => reject(new Error('Failed to load explosion image'));
        // 随机选择 sz.jpg 或 ss.jpg
        const randomImage = Math.random() < 0.5 ? './js/sz.jpg' : './js/ss.jpg';
        img.src = randomImage;
    });
}

// 爆炸效果对象
export class Explosion {
    constructor(x, y) {
        this.x = x;
        this.y = y;
        this.startTime = Date.now();
        // 每次创建爆炸效果时重新加载图像
        this.explosionImage = null;
        loadExplosionImage().then(img => {
            this.explosionImage = img;
        });
    }

    draw() {
        if (this.explosionImage && this.explosionImage.complete) {
            ctx.drawImage(this.explosionImage, this.x - 50, this.y - 50, 100, 100);
        }
    }

    isExpired() {
        return Date.now() - this.startTime > 3000;
    }
}

// 爆炸效果管理器
export class ExplosionManager {
    constructor() {
        this.explosions = [];
    }

    addExplosion(x, y) {
        this.explosions.push(new Explosion(x, y));
    }

    update() {
        this.explosions = this.explosions.filter(explosion => !explosion.isExpired());
    }

    draw() {
        this.explosions.forEach(explosion => explosion.draw());
    }
}
